﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Dictionary;
using OutdoorLib.BaseEntity.Infrastructure;

namespace OutdoorLib.BaseEntity.Agency
{
    /// <summary>
    /// Настроечная информация
    /// </summary>
    public class SettingInfo : EntitySingle
    {
        private SiteMapItems _SiteMapItems = new SiteMapItems();
        private TypeMapItems _TypeMapItems = new TypeMapItems();
        private DictionaryInfo _DictionaryInfo = new DictionaryInfo();

        public override void Load()
        {
            _DictionaryInfo.CategoryList.Clear();
            _DictionaryInfo.DictionaryList.Clear();

            base.Load();
        }
        
        public string AgencyID { get; set; }

        
        public DictionaryInfo DictionaryInfo { get { return _DictionaryInfo; } set { _DictionaryInfo = value; } }

        /// <summary>
        /// Карта типов
        /// </summary>
        public TypeMapItems TypeMapItems { get { return _TypeMapItems; } }

        /// <summary>
        //// Карта страничек
        /// </summary>
        public SiteMapItems SiteMapItems { get { return _SiteMapItems; } }

        
       
    }

    /// <summary>
    /// Карта типов 
    /// </summary>
    public class TypeMapItems : EntityList
    {        
        
    }
        


    public class DictionaryInfo : EntitySingle
    {
        private DictionaryList _DictionaryList = new DictionaryList();
        public DictionaryList DictionaryList { get { return _DictionaryList; } set { _DictionaryList = value; } }

        private EntityList  _CategoryList = new EntityList();
        /// <summary>
        /// Категории справочников - список DictionaryCategoryItem 
        /// </summary>
        public EntityList CategoryList { get { return _CategoryList; } set { _CategoryList = value; } }
    }
    public class DictionaryList : EntityList
    {
        private class cmp: IEqualityComparer <DictionaryItem>
        {
            #region IEqualityComparer<DictionaryItem> Members

            public bool Equals(DictionaryItem x, DictionaryItem y)
            {
                if (x.ParamID.Equals(y.ParamID))
                    return true;
                else
                    return false;
            }

            public int GetHashCode(DictionaryItem obj)
            {
                Func<string, Int32> f = delegate(string s)
                {
                    Int32 res = 0;
                    foreach (char i in s.ToCharArray())
                    {
                        res = res + (int)i;

                    }
                    return res;
                };
                DictionaryItem v = (DictionaryItem)obj;
                return f(v.Value.ToString());
                
            }

            #endregion
        }
        /// <summary>
        /// Вернуть существующие типы 
        /// </summary>
        public List<DictionaryItem> DictionaryTypes 
        { 
            get{
                List<DictionaryItem> result = new List<DictionaryItem>();
                for (int i = 0; i < this.Count; i++)
                {
                    DictionaryItem itm = new DictionaryItem();
                    itm.ParamID = (this[i] as TreeDictionaryItem).TypeID;
                    itm.Value = (this[i] as TreeDictionaryItem).Name;
                    result.Add(itm);
                }
                cmp c = new cmp();
                return result.Distinct<DictionaryItem>(c).ToList <DictionaryItem>();
            }
        }      
    }

    
}
